package com.king.cms.log;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException; 

import com.king.cms.action.BaseDispatchAction;
import com.king.cms.common.InitSystem;
import com.king.cms.common.SessionUtil;
import com.king.cms.db.QueryPage;
import com.king.cms.log.LogService;
import com.king.cms.orm.CmsUser;
 

public class TraceAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(TraceAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws ServletException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException {
		String type = request.getParameter("type");
		String action = request.getParameter("action");
		String sql = "";
		if("list".equals(action)){
			if("delete".equals(type))
				sql =InitSystem.getBpmParam("log.select_by_delete")+ " and log.LOG_TYPE= 2 ";
			if("modify".equals(type))
				sql =InitSystem.getBpmParam("log.select_by_type")+ " and log.LOG_TYPE=1 ";
			CmsUser user = SessionUtil.getCmsUser(request);
			String uid = SessionUtil.getUserId(request )+"";
			if(null != user &&user.getIsAdmin()== null)
				sql+=" and LOG_USER = " +uid ;
			sql+=" ORDER BY  LOG_TIME DESC";
			QueryPage query = new QueryPage(request, request.getContextPath()+"/trace.do?action="+ action +"&type="+type ,sql );
			List dataList = query.getDataList(); 
			request.setAttribute("dataList", dataList);
			request.setAttribute("pageStr", query.getPageStr());
			request.setAttribute("query", query);
			if("modify".equals(type))
			return   mapping.findForward("trace.modify");
			if("delete".equals(type))
				return   mapping.findForward("trace.delete");
		}
		if("delete".equals(action)){
			String[] traceId = request.getParameterValues("traceId");
			if(traceId != null){
				for(int i = 0;i<traceId.length;i++){
					String id = traceId[i];
				 this.getDBOperator(request).update(InitSystem.getBpmParam("log.delete"), new Object[]{new Integer(id)});
				}
			}
		    return new ActionForward("/trace.do?action=list&type="+type);
		}
		return   mapping.findForward("trace.modify");
	 
	}
}
